from enum import Enum
from typing import List, Dict, Optional, Any, Union
from pydantic import BaseModel, Field
from app.schemas.facenet_base_schema import TaskStatus


class GraphNode(BaseModel):
    """图节点定义"""
    id: str
    type: Optional[str] = "unknown"
    properties: Optional[Dict[str, Any]] = None


class GraphEdge(BaseModel):
    """图边定义"""
    source: str
    target: str
    type: Optional[str] = "default"
    properties: Optional[Dict[str, Any]] = None


class Graph(BaseModel):
    """图结构定义"""
    nodes: List[GraphNode]
    edges: List[GraphEdge]


class EventPattern(BaseModel):
    """事件模式定义"""
    node_changes: Dict[str, int]
    edge_changes: Dict[str, int]


class DetectedEvent(BaseModel):
    """检测到的事件"""
    time_idx: int
    pattern: EventPattern
    affected_nodes: List[str]
    affected_edges: List[List[str]]
    score: float
    models_score: Optional[List[float]] = None
    combined_score: Optional[float] = None
    is_anomaly: Optional[bool] = None
    total_changes: Optional[int] = None


class EventExplanation(BaseModel):
    """事件解释"""
    summary: str
    affected_entities: Dict[str, int]
    changes: Dict[str, Dict[str, int]]


class DHGEDParams(BaseModel):
    """DHGED服务参数"""
    embedding_dim: int = 64
    hidden_dim: int = 32
    window_size: int = 5
    anomaly_threshold: float = 0.9


class DHGEDRequest(BaseModel):
    """DHGED请求"""
    task_id: str
    task_callback_url: Optional[str] = None
    input_params: Dict[str, Any]


class DHGEDFitRequest(DHGEDRequest):
    """模型训练请求"""
    input_params: Dict[str, Any] = Field(..., example={
        "graph_sequence": List[Graph],
        "model_params": DHGEDParams
    })


class DHGEDDetectRequest(DHGEDRequest):
    """事件检测请求"""
    input_params: Dict[str, Any] = Field(..., example={
        "new_graph": Graph,
        "previous_graphs": Optional[List[Graph]]
    })


class DHGEDExplainRequest(DHGEDRequest):
    """事件解释请求"""
    input_params: Dict[str, Any] = Field(..., example={
        "event": DetectedEvent
    })


class DHGEDResponse(BaseModel):
    """DHGED服务响应"""
    task_id: str
    task_status: TaskStatus
    task_progress: float = 0
    output_params: Optional[Dict[str, Any]] = None
    error_message: Optional[str] = None


class DHGEDMiddleResponse(BaseModel):
    """DHGED服务中间响应"""
    task_id: str
    task_callback_url: Optional[str] = None
    task_status: TaskStatus
    task_progress: float = 0
    input_params: Dict[str, Any]
    metrics: List[Dict[str, Any]] = [] 